home *** CD-ROM | disk | FTP | other *** search
Wrap
package com.sun.java.swing.text; import com.sun.java.accessibility.Accessible; import com.sun.java.accessibility.AccessibleContext; import com.sun.java.swing.Action; import com.sun.java.swing.JComponent; import com.sun.java.swing.KeyStroke; import com.sun.java.swing.Scrollable; import com.sun.java.swing.UIManager; import com.sun.java.swing.event.CaretEvent; import com.sun.java.swing.event.CaretListener; import com.sun.java.swing.event.EventListenerList; import com.sun.java.swing.plaf.TextUI; import java.awt.AWTEvent; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.LayoutManager; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Reader; import java.io.Writer; import java.util.Hashtable; public abstract class JTextComponent extends JComponent implements Scrollable, Accessible { public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey"; private Document model; private transient Caret caret; private transient Highlighter highlighter; private transient Keymap keymap; private boolean opaque; private transient MutableCaretEvent caretEvent; private Color caretColor; private Color selectionColor; private Color selectedTextColor; private Color disabledTextColor; private boolean editable; private Insets margin; private char focusAccelerator; private Action focusAction = new FocusAction(this); private static ClipboardOwner defaultClipboardOwner = new ClipboardObserver(); private static Hashtable keymapTable = null; private JTextComponent editor; private static JTextComponent focusedComponent; public static final String DEFAULT_KEYMAP = "default"; static final KeyBinding[] defaultBindings = new KeyBinding[]{new KeyBinding(KeyStroke.getKeyStroke(8, 0), "delete-previous"), new KeyBinding(KeyStroke.getKeyStroke(127, 0), "delete-next"), new KeyBinding(KeyStroke.getKeyStroke(39, 0), "caret-forward"), new KeyBinding(KeyStroke.getKeyStroke(37, 0), "caret-backward")}; static Class class$com$sun$java$swing$event$CaretListener; static { try { keymapTable = new Hashtable(17); Keymap binding = addKeymap("default", (Keymap)null); binding.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction()); EditorKit kit = new DefaultEditorKit(); loadKeymap(binding, defaultBindings, kit.getActions()); } catch (Throwable var2) { var2.printStackTrace(); keymapTable = new Hashtable(17); } } public JTextComponent() { ((Component)this).enableEvents(8L); this.caretEvent = new MutableCaretEvent(this); ((Component)this).addMouseListener(this.caretEvent); ((Component)this).addFocusListener(this.caretEvent); this.setEditable(true); ((Container)this).setLayout((LayoutManager)null); this.updateUI(); } public void addCaretListener(CaretListener listener) { EventListenerList var10000 = super.listenerList; Class var10001 = class$com$sun$java$swing$event$CaretListener; if (var10001 == null) { try { var10001 = Class.forName("com.sun.java.swing.event.CaretListener"); } catch (ClassNotFoundException var2) { throw new NoClassDefFoundError(((Throwable)var2).getMessage()); } class$com$sun$java$swing$event$CaretListener = var10001; } var10000.add(var10001, listener); } public static Keymap addKeymap(String nm, Keymap parent) { Keymap map = new DefaultKeymap(nm, parent); if (nm != null) { keymapTable.put(nm, map); } return map; } public void copy() { try { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); int p0 = Math.min(this.caret.getDot(), this.caret.getMark()); int p1 = Math.max(this.caret.getDot(), this.caret.getMark()); if (p0 != p1) { Document doc = this.getDocument(); String srcData = doc.getText(p0, p1 - p0); StringSelection contents = new StringSelection(srcData); clipboard.setContents(contents, defaultClipboardOwner); } } catch (BadLocationException var7) { } } public void cut() { try { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); int p0 = Math.min(this.caret.getDot(), this.caret.getMark()); int p1 = Math.max(this.caret.getDot(), this.caret.getMark()); if (p0 != p1) { Document doc = this.getDocument(); String srcData = doc.getText(p0, p1 - p0); StringSelection contents = new StringSelection(srcData); clipboard.setContents(contents, defaultClipboardOwner); doc.remove(p0, p1 - p0); } } catch (BadLocationException var7) { } } protected void fireCaretUpdate(CaretEvent e) { Object[] listeners = super.listenerList.getListenerList(); for(int i = listeners.length - 2; i >= 0; i -= 2) { Object var10000 = listeners[i]; Class var10001 = class$com$sun$java$swing$event$CaretListener; if (var10001 == null) { try { var10001 = Class.forName("com.sun.java.swing.event.CaretListener"); } catch (ClassNotFoundException var4) { throw new NoClassDefFoundError(((Throwable)var4).getMessage()); } class$com$sun$java$swing$event$CaretListener = var10001; } if (var10000 == var10001) { ((CaretListener)listeners[i + 1]).caretUpdate(e); } } } public AccessibleContext getAccessibleContext() { if (super.accessibleContext == null) { super.accessibleContext = new AccessibleJTextComponent(this); } return super.accessibleContext; } public Action[] getActions() { return this.getUI().getEditorKit().getActions(); } public Caret getCaret() { return this.caret; } public Color getCaretColor() { return this.caretColor; } public int getCaretPosition() { return this.caret.getDot(); } public Color getDisabledTextColor() { return this.disabledTextColor; } public Document getDocument() { return this.model; } public char getFocusAccelerator() { return this.focusAccelerator; } static final JTextComponent getFocusedComponent() { return focusedComponent; } public Highlighter getHighlighter() { return this.highlighter; } public Keymap getKeymap() { return this.keymap; } public static Keymap getKeymap(String nm) { return (Keymap)keymapTable.get(nm); } public Insets getMargin() { return this.margin == null ? ((TextUI)super.ui).getDefaultMargin() : this.margin; } public Dimension getPreferredScrollableViewportSize() { return ((JComponent)this).getPreferredSize(); } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { switch (orientation) { case 0: return visibleRect.width; case 1: return visibleRect.height; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } public boolean getScrollableTracksViewportHeight() { return false; } public boolean getScrollableTracksViewportWidth() { return false; } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { switch (orientation) { case 0: return visibleRect.width / 10; case 1: return visibleRect.height / 10; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } public String getSelectedText() { String txt = null; int p0 = Math.min(this.caret.getDot(), this.caret.getMark()); int p1 = Math.max(this.caret.getDot(), this.caret.getMark()); if (p0 != p1) { try { Document doc = this.getDocument(); txt = doc.getText(p0, p1 - p0); } catch (BadLocationException var5) { throw new IllegalArgumentException(((Throwable)var5).getMessage()); } } return txt; } public Color getSelectedTextColor() { return this.selectedTextColor; } public Color getSelectionColor() { return this.selectionColor; } public int getSelectionEnd() { int end = Math.max(this.caret.getDot(), this.caret.getMark()); return end; } public int getSelectionStart() { int start = Math.min(this.caret.getDot(), this.caret.getMark()); return start; } public String getText() { Document doc = this.getDocument(); String txt; try { txt = doc.getText(0, doc.getLength()); } catch (BadLocationException var3) { txt = null; } return txt; } public String getText(int offs, int len) throws BadLocationException { return this.getDocument().getText(offs, len); } public TextUI getUI() { return (TextUI)super.ui; } public boolean isEditable() { return this.editable; } public boolean isFocusTraversable() { return ((Component)this).isEnabled(); } public boolean isOpaque() { return this.opaque; } public static void loadKeymap(Keymap map, KeyBinding[] bindings, Action[] actions) { Hashtable h = new Hashtable(); for(int i = 0; i < actions.length; ++i) { Action a = actions[i]; String value = (String)a.getValue("Name"); h.put(value != null ? value : "", a); } for(int i = 0; i < bindings.length; ++i) { Action a = (Action)h.get(bindings[i].actionName); if (a != null) { map.addActionForKeyStroke(bindings[i].key, a); } } } private final boolean mapEventToAction(KeyEvent e) { Keymap binding = this.getKeymap(); if (binding != null) { KeyStroke k = KeyStroke.getKeyStrokeForEvent(e); Action a = binding.getAction(k); if (a != null) { String command = null; if (e.getKeyChar() != 0) { command = String.valueOf(e.getKeyChar()); } ActionEvent ae = new ActionEvent(this, 1001, command, ((InputEvent)e).getModifiers()); a.actionPerformed(ae); ((InputEvent)e).consume(); return true; } } return false; } public Rectangle modelToView(int pos) throws BadLocationException { return this.getUI().modelToView(pos); } public void moveCaretPosition(int pos) { this.caret.moveDot(pos); } public void paste() { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable content = clipboard.getContents(this); if (content != null) { try { String dstData = (String)content.getTransferData(DataFlavor.stringFlavor); this.replaceSelection(dstData); } catch (Exception var4) { System.err.println("Couldn't get clipboard contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName()); } } } protected void processComponentKeyEvent(KeyEvent e) { int id = ((AWTEvent)e).getID(); switch (id) { case 400: if (!this.mapEventToAction(e)) { Keymap binding = this.getKeymap(); if (binding != null) { Action a = binding.getDefaultAction(); if (a != null) { ActionEvent ae = new ActionEvent(this, 1001, String.valueOf(e.getKeyChar()), ((InputEvent)e).getModifiers()); a.actionPerformed(ae); ((InputEvent)e).consume(); } } } break; case 401: this.mapEventToAction(e); break; case 402: this.mapEventToAction(e); } } public void read(Reader in, Object desc) throws IOException { EditorKit kit = this.getUI().getEditorKit(); Document doc = kit.createDefaultDocument(); if (desc != null) { doc.putProperty("stream", desc); } try { kit.read(in, doc, 0); this.setDocument(doc); } catch (BadLocationException var6) { throw new IOException(((Throwable)var6).getMessage()); } } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); this.caretEvent = new MutableCaretEvent(this); ((Component)this).addMouseListener(this.caretEvent); ((Component)this).addFocusListener(this.caretEvent); this.getUI().installUI(this); } public void removeCaretListener(CaretListener listener) { EventListenerList var10000 = super.listenerList; Class var10001 = class$com$sun$java$swing$event$CaretListener; if (var10001 == null) { try { var10001 = Class.forName("com.sun.java.swing.event.CaretListener"); } catch (ClassNotFoundException var2) { throw new NoClassDefFoundError(((Throwable)var2).getMessage()); } class$com$sun$java$swing$event$CaretListener = var10001; } var10000.remove(var10001, listener); } public static Keymap removeKeymap(String nm) { return (Keymap)keymapTable.remove(nm); } public void replaceSelection(String content) { if (!this.isEditable()) { ((Component)this).getToolkit().beep(); } else { Document doc = this.getDocument(); if (doc != null) { try { int p0 = Math.min(this.caret.getDot(), this.caret.getMark()); int p1 = Math.max(this.caret.getDot(), this.caret.getMark()); if (p0 != p1) { doc.remove(p0, p1 - p0); } if (content != null && content.length() > 0) { doc.insertString(p0, content, (AttributeSet)null); } } catch (BadLocationException var5) { ((Component)this).getToolkit().beep(); } } } } public void select(int selectionStart, int selectionEnd) { this.setCaretPosition(selectionStart); this.moveCaretPosition(selectionEnd); } public void selectAll() { Document doc = this.getDocument(); if (doc != null) { this.setCaretPosition(0); this.moveCaretPosition(doc.getLength()); } } public void setCaret(Caret c) { if (this.caret != null) { this.caret.removeChangeListener(this.caretEvent); this.caret.deinstall(this); } Caret old = this.caret; this.caret = c; if (this.caret != null) { this.caret.install(this); this.caret.addChangeListener(this.caretEvent); } ((JComponent)this).firePropertyChange("caret", old, this.caret); } public void setCaretColor(Color c) { Color old = this.caretColor; this.caretColor = c; ((JComponent)this).firePropertyChange("caretColor", old, this.caretColor); } public void setCaretPosition(int position) { Document doc = this.getDocument(); if (doc != null) { if (position > doc.getLength() || position < 0) { throw new IllegalArgumentException("bad position: " + position); } this.caret.setDot(position); } } public void setDisabledTextColor(Color c) { Color old = this.disabledTextColor; this.disabledTextColor = c; ((JComponent)this).firePropertyChange("disabledTextColor", old, this.disabledTextColor); } public void setDocument(Document doc) { if (super.accessibleContext != null) { this.model.removeDocumentListener((AccessibleJTextComponent)super.accessibleContext); } Document old = this.model; this.model = doc; ((JComponent)this).firePropertyChange("document", old, doc); ((JComponent)this).revalidate(); ((Component)this).repaint(); if (super.accessibleContext != null) { this.model.addDocumentListener((AccessibleJTextComponent)super.accessibleContext); } } public void setEditable(boolean b) { boolean oldVal = this.editable; this.editable = b; ((JComponent)this).firePropertyChange("editable", new Boolean(oldVal), new Boolean(this.editable)); } public void setEnabled(boolean b) { super.setEnabled(b); ((Component)this).repaint(); } public void setFocusAccelerator(char aKey) { aKey = Character.toUpperCase(aKey); KeyStroke[] keyStrokes = ((JComponent)this).getRegisteredKeyStrokes(); int i = 0; for(int c = keyStrokes.length; i < c; ++i) { if (((JComponent)this).getActionForKeyStroke(keyStrokes[i]) == this.focusAction) { if (keyStrokes[i].getKeyChar() == aKey) { return; } ((JComponent)this).unregisterKeyboardAction(keyStrokes[i]); break; } } if (aKey != 0) { ((JComponent)this).registerKeyboardAction(this.focusAction, KeyStroke.getKeyStroke(aKey, 8), 2); } char old = this.focusAccelerator; this.focusAccelerator = aKey; ((JComponent)this).firePropertyChange("focusAcceleratorKey", old, this.focusAccelerator); } public void setHighlighter(Highlighter h) { if (this.highlighter != null) { this.highlighter.deinstall(this); } Highlighter old = this.highlighter; this.highlighter = h; if (this.highlighter != null) { this.highlighter.install(this); } ((JComponent)this).firePropertyChange("highlighter", old, h); } public void setKeymap(Keymap map) { Keymap old = this.keymap; this.keymap = map; ((JComponent)this).firePropertyChange("keymap", old, this.keymap); } public void setMargin(Insets m) { Insets old = this.margin; this.margin = m; ((JComponent)this).firePropertyChange("margin", old, m); ((Container)this).invalidate(); } public void setOpaque(boolean o) { this.opaque = o; } public void setSelectedTextColor(Color c) { Color old = this.selectedTextColor; this.selectedTextColor = c; ((JComponent)this).firePropertyChange("selectedTextColor", old, this.selectedTextColor); } public void setSelectionColor(Color c) { Color old = this.selectionColor; this.selectionColor = c; ((JComponent)this).firePropertyChange("selectionColor", old, this.selectionColor); } public void setSelectionEnd(int selectionEnd) { this.select(this.getSelectionStart(), selectionEnd); } public void setSelectionStart(int selectionStart) { this.select(selectionStart, this.getSelectionEnd()); } public void setText(String t) { try { Document doc = this.getDocument(); doc.remove(0, doc.getLength()); doc.insertString(0, t, (AttributeSet)null); } catch (BadLocationException var3) { ((Component)this).getToolkit().beep(); } } public void setUI(TextUI ui) { super.setUI(ui); } public void updateUI() { this.setUI((TextUI)UIManager.getUI(this)); ((Container)this).invalidate(); } public int viewToModel(Point pt) { return this.getUI().viewToModel(pt); } public void write(Writer out) throws IOException { Document doc = this.getDocument(); try { this.getUI().getEditorKit().write(out, doc, 0, doc.getLength()); } catch (BadLocationException var4) { throw new IOException(((Throwable)var4).getMessage()); } } static Document access$model(JTextComponent var0) { return var0.model; } static void access$focusedComponent(JTextComponent var0) { focusedComponent = var0; } }